package com.ls.lserp.modules.material.service;




import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.CollectionsUtils;
import com.ls.lserp.common.utils.ResourcesUtil;
import com.ls.lserp.common.utils.SpringContextHolder;
import com.ls.lserp.modules.material.dao.MaterialInboundDao;
import com.ls.lserp.modules.material.dao.StockCheckDao;
import com.ls.lserp.modules.material.entity.InventoryRequest;
import com.ls.lserp.modules.material.entity.MaterialOutInbound;
import com.ls.lserp.modules.material.entity.StockCheck;
import com.ls.lserp.modules.material.entity.StockCheckExportEntity;
import com.ls.lserp.modules.sys.utils.DictUtils;

@Service
@Transactional(readOnly = false)
public class StockCheckService extends CrudService<StockCheckDao,StockCheck>{

	//合同
	 @Autowired
	    private StockCheckDao stockCheckDao ;

	 /**
		 * @Title: findList
		 * @Description: 一览查询
		 * @param page
		 * @param orderMateriel
		 * @return Page<StockCheck>
		 */
		public Page<StockCheck> findList(Page<StockCheck> page, StockCheck stockCheck) {
			
			
	        // 执行分页查询
	        page.setList(stockCheckDao.findList(stockCheck));
	        
	        return page;
		}
		/**
		 * check BigDecimal
		 * @param cost
		 * @return 取不到返回0
		 */
		public BigDecimal checkBigDecimalNull(BigDecimal cost){
			if(cost==null){
				return new BigDecimal("0");
			}
			return cost;
		}
		
		/**
		   * @Title: saveData
		   * @Description: 保存库存盘点
		   * @param stockCheck
		   * @return void 
		   * @throws Exception
		   */
		  @Transactional(readOnly = false)
		  public void saveData(StockCheck stockCheck) throws Exception {
			  stockCheck.preInsert();
			  stockCheckDao.insert(stockCheck);
			  List<StockCheck> quantityList = stockCheckDao.findQuantityList(stockCheck);
			  if (quantityList != null && quantityList.size()>0) {
				  for (int i = 0; i < quantityList.size(); i++) {
					  quantityList.get(i).setId(stockCheck.getId());
					  stockCheckDao.insertDetails(quantityList.get(i));
				  }
			  }
			  stockCheck.setEnableflag(0);
			  stockCheckDao.updateWarehouseStatus(stockCheck);
		  }
		  
		  /**
		   * @Title: saveData
		   * @Description: 盘点完成
		   * @param stockCheck
		   * @return void 
		   * @throws Exception
		   */
		  @Transactional(readOnly = false)
		  public void checkComplete(StockCheck stockCheck) throws Exception {
			  stockCheck.preUpdate();
			  stockCheckDao.updateTakeStatus(stockCheck);
			  stockCheck.setEnableflag(1);
			  stockCheckDao.updateWarehouseStatus(stockCheck);
		  }
		  
		  /**
		   * @Title: insertExcelData
		   * @Description: 更新库存盘点明细
		   * @param @param stockCheck
		   * @return void 
		   * @throws Exception
		   */
		  @Transactional(readOnly = false)
		  public void insertExcelData(StockCheck stockCheck) throws Exception {
			  List<StockCheck> stockChecks = stockCheck.getStockCheckList();
			  List<StockCheck> takeList = stockCheckDao.findStockList(stockCheck);
			  for (StockCheck sc:takeList) {
				for (int i = 0; i < stockChecks.size(); i++) {
					if (sc.getWarehouseID().equals(stockChecks.get(i).getWarehouseID())&&sc.getRackID().equals(stockChecks.get(i).getRackID())&&sc.getMaterialId().equals(stockChecks.get(i).getMaterialId())) {
						sc.setRealQuantity(stockChecks.get(i).getRealQuantity());
						sc.setTakeQuantity(sc.getCheckQuantity().subtract(stockChecks.get(i).getRealQuantity()));
						if (sc.getCheckQuantity().compareTo(stockChecks.get(i).getRealQuantity())==0) {
							sc.setCheckStatus("TAKE_STATUS-EQUAL");
						}else if (sc.getCheckQuantity().compareTo(stockChecks.get(i).getRealQuantity())==-1) {
							sc.setCheckStatus("TAKE_STATUS-OVERAGE");
						}else {
							sc.setCheckStatus("TAKE_STATUS-SHORTAGE");
						}
						sc.preUpdate();
						stockCheckDao.updateTakeDetail(sc);
					}
				}
			}
		  }
		  
		
	   /**
		  * @Title: findWarehouseList
		  * @Description: 仓库集
		  * @param @param stockCheck
		  * @param @return
		  * @return List<StockCheck> 
		  * @throws
		  */
		public List<StockCheck> findWarehouseList(StockCheck stockCheck){
			return stockCheckDao.findWarehouseList(stockCheck);
		}
		/**
		  * @Title: findRackList
		  * @Description: 货架集
		  * @param @param stockCheck
		  * @param @return
		  * @return List<StockCheck> 
		  * @throws
		  */
		public List<StockCheck> findRackList(StockCheck stockCheck){
			return stockCheckDao.findRackList(stockCheck);
		}
		
		/**
		  * @Title: findRackList
		  * @Description: 盘点明细
		  * @param @param stockCheck
		  * @param @return
		  * @return List<StockCheck> 
		  * @throws
		  */
		public List<StockCheck> findTakeDetailList(StockCheck stockCheck){
			return stockCheckDao.findTakeDetailList(stockCheck);
		}
		
		/**
		  * @Title: findRackList
		  * @Description: 导出
		  * @param @param stockCheck
		  * @param @return
		  * @return List<StockCheck> 
		  * @throws
		  */
		public List<StockCheckExportEntity> makeStockCheckList(StockCheck stockCheck){
			List<StockCheckExportEntity> stockCheckList =  stockCheckDao.makeStockCheckList(stockCheck);
			if (stockCheckList != null && stockCheckList.size() > 0) {
				for(StockCheckExportEntity sEntity : stockCheckList){
					//单位
					if (sEntity.getMaterialUnitCd() != null && !"".equals(sEntity.getMaterialUnitCd())) {
						sEntity.setMaterialUnitCd(sEntity.getMaterialUnitCd() + ":" + DictUtils.getDictName(sEntity.getMaterialUnitCd()));
					}
					//季节
					if (sEntity.getMaterialSeason() != null && !"".equals(sEntity.getMaterialSeason())) {
						sEntity.setMaterialSeason(sEntity.getMaterialSeason() + ":" + DictUtils.getDictName(sEntity.getMaterialSeason()));
					}
					//等级
					if (sEntity.getMaterialLevel() != null && !"".equals(sEntity.getMaterialLevel())) {
						sEntity.setMaterialLevel(sEntity.getMaterialLevel() + ":" + DictUtils.getDictName(sEntity.getMaterialLevel()));
					}
					//织法
					if (sEntity.getMaterialKnitMethod() != null && !"".equals(sEntity.getMaterialKnitMethod())) {
						sEntity.setMaterialKnitMethod(sEntity.getMaterialKnitMethod() + ":" + DictUtils.getDictName(sEntity.getMaterialKnitMethod()));
					}
					//来源
					if (sEntity.getMaterialSource() != null && !"".equals(sEntity.getMaterialSource())) {
						sEntity.setMaterialSource(sEntity.getMaterialSource() + ":" + DictUtils.getDictName(sEntity.getMaterialSource()));
					}
					//风格
					if (sEntity.getMaterialStyle() != null && !"".equals(sEntity.getMaterialStyle())) {
						sEntity.setMaterialStyle(sEntity.getMaterialStyle() + ":" + DictUtils.getDictName(sEntity.getMaterialStyle()));
					}
					//用途
					if (sEntity.getMaterialUseType() != null && !"".equals(sEntity.getMaterialUseType())) {
						sEntity.setMaterialUseType(sEntity.getMaterialUseType() + ":" + DictUtils.getDictName(sEntity.getMaterialUseType()));
					}
				}
			}
			return stockCheckList;
		}
		
		
		     
}
